﻿
Imports Microsoft.ApplicationBlocks.Data

Public Class Dvendedor

    Public Shared Function SeleccionarTodo(ByVal descEmpresa As String) As List(Of Evendedor)
        Dim vendedors As List(Of Evendedor) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_vendedor_sel", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            vendedors = New List(Of Evendedor)
                            Dim vendedor As Evendedor = Nothing
                            Do While dr.Read
                                vendedor = New Evendedor( _
                                    dr.GetInt32(dr.GetOrdinal("idVendedor")), _
                                    dr.GetString(dr.GetOrdinal("nombre")), _
                                    dr.GetString(dr.GetOrdinal("apePaterno")), _
                                    dr.GetString(dr.GetOrdinal("apeMaterno")), _
                                    dr.GetInt32(dr.GetOrdinal("idTpoDcmto")), _
                                    dr.GetString(dr.GetOrdinal("descTpoDcmto")), _
                                    dr.GetString(dr.GetOrdinal("nroDcmto")), _
                                    dr.GetString(dr.GetOrdinal("telefono")), _
                                    dr.GetString(dr.GetOrdinal("movil")), _
                                    dr.GetDecimal(dr.GetOrdinal("comisionProforma")), _
                                    dr.GetDecimal(dr.GetOrdinal("comisionSeparacion")), _
                                    dr.GetDecimal(dr.GetOrdinal("comisionVenta")), _
                                    dr.GetString(dr.GetOrdinal("email")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                                vendedors.Add(vendedor)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return vendedors
    End Function
    'Public Shared Function SeleccionarTodoXML(ByVal descEmpresa As String) As System.Xml.XmlReader
    '    Dim cmd As New SqlXmlCommand("Provider=SQLOLEDB;Server=(local);database=EVOLTA; Integrated Security=SSPI") '"usp_vendedor_selXML",
    '    cmd.CommandType = SqlXmlCommandType.Sql
    '    cmd.RootTag = "TvendedorList"
    '    cmd.ClientSideXml = True
    '    cmd.CommandText = "EXEC usp_vendedor_selXML"
    '    Try
    '        Dim dr As System.Xml.XmlReader = cmd.ExecuteXmlReader()
    '        dr.Close()
    '        Return dr
    '    Catch ex As Exception
    '        Throw
    '    End Try
    'End Function
    Public Shared Function SeleccionarXEstado(ByVal descEmpresa As String) As List(Of Evendedor)
        Dim vendedors As List(Of Evendedor) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_vendedor_selXEstado", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            vendedors = New List(Of Evendedor)
                            Dim vendedor As Evendedor = Nothing 'tipo 
                            Do While dr.Read
                                vendedor = New Evendedor( _
                                    dr.GetInt32(dr.GetOrdinal("idVendedor")), _
                                    dr.GetString(dr.GetOrdinal("nombre")), _
                                    dr.GetString(dr.GetOrdinal("apePaterno")), _
                                    dr.GetString(dr.GetOrdinal("apeMaterno")), _
                                    dr.GetInt32(dr.GetOrdinal("idTpoDcmto")), _
                                    dr.GetString(dr.GetOrdinal("descTpoDcmto")), _
                                    dr.GetString(dr.GetOrdinal("nroDcmto")), _
                                    dr.GetString(dr.GetOrdinal("telefono")), _
                                    dr.GetString(dr.GetOrdinal("movil")), _
                                    dr.GetDecimal(dr.GetOrdinal("comisionProforma")), _
                                    dr.GetDecimal(dr.GetOrdinal("comisionSeparacion")), _
                                    dr.GetDecimal(dr.GetOrdinal("comisionVenta")), _
                                    dr.GetString(dr.GetOrdinal("email")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                                vendedors.Add(vendedor)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return vendedors
    End Function
    Public Shared Function SeleccionarXUsuario(ByVal descEmpresa As String, ByVal idUsuario As Integer) As Evendedor
        Dim vendedor As Evendedor = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_vendedor_selXEstado", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = idUsuario
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then

                            Do While dr.Read
                                vendedor = New Evendedor( _
                                    dr.GetInt32(dr.GetOrdinal("idVendedor")), _
                                    dr.GetString(dr.GetOrdinal("nombre")), _
                                    dr.GetString(dr.GetOrdinal("apePaterno")), _
                                    dr.GetString(dr.GetOrdinal("apeMaterno")), _
                                    dr.GetInt32(dr.GetOrdinal("idTpoDcmto")), _
                                    dr.GetString(dr.GetOrdinal("descTpoDcmto")), _
                                    dr.GetString(dr.GetOrdinal("nroDcmto")), _
                                    dr.GetString(dr.GetOrdinal("telefono")), _
                                    dr.GetString(dr.GetOrdinal("movil")), _
                                    dr.GetDecimal(dr.GetOrdinal("comisionProforma")), _
                                    dr.GetDecimal(dr.GetOrdinal("comisionSeparacion")), _
                                    dr.GetDecimal(dr.GetOrdinal("comisionVenta")), _
                                    dr.GetString(dr.GetOrdinal("email")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return vendedor
    End Function
    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal pVendedor As Evendedor) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_vendedor_add", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@nombre", SqlDbType.VarChar, 55).Value = pVendedor.nombre
                cmd.Parameters.Add("@apePaterno", SqlDbType.VarChar, 55).Value = pVendedor.apePaterno
                cmd.Parameters.Add("@apeMaterno", SqlDbType.VarChar, 55).Value = pVendedor.apeMaterno
                cmd.Parameters.Add("@idTpoDcmto", SqlDbType.Int).Value = pVendedor.EtpoDocmto.idTpoDcmto
                cmd.Parameters.Add("@nroDcmto", SqlDbType.VarChar, 55).Value = pVendedor.nroDcmto
                cmd.Parameters.Add("@telefono", SqlDbType.VarChar, 11).Value = pVendedor.telefono
                cmd.Parameters.Add("@movil", SqlDbType.VarChar, 11).Value = pVendedor.movil
                cmd.Parameters.Add("@comisionProforma", SqlDbType.Decimal, 15, 2).Value = pVendedor.comisionProforma
                cmd.Parameters.Add("@comisionSeparacion", SqlDbType.Decimal, 15, 2).Value = pVendedor.comisionSeparacion
                cmd.Parameters.Add("@comisionVenta", SqlDbType.Decimal, 15, 2).Value = pVendedor.comisionVenta
                cmd.Parameters.Add("@email", SqlDbType.VarChar, 40).Value = pVendedor.email
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pVendedor.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Editar(ByVal descEmpresa As String, ByVal pVendedor As Evendedor) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_vendedor_edit", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idVendedor", SqlDbType.Int).Value = pVendedor.idVendedor
                cmd.Parameters.Add("@nombre", SqlDbType.VarChar, 55).Value = pVendedor.nombre
                cmd.Parameters.Add("@apePaterno", SqlDbType.VarChar, 55).Value = pVendedor.apePaterno
                cmd.Parameters.Add("@apeMaterno", SqlDbType.VarChar, 55).Value = pVendedor.apeMaterno
                cmd.Parameters.Add("@idTpoDcmto", SqlDbType.Int).Value = pVendedor.EtpoDocmto.idTpoDcmto
                cmd.Parameters.Add("@nroDcmto", SqlDbType.VarChar, 55).Value = pVendedor.nroDcmto
                cmd.Parameters.Add("@telefono", SqlDbType.VarChar, 11).Value = pVendedor.telefono
                cmd.Parameters.Add("@movil", SqlDbType.VarChar, 11).Value = pVendedor.movil
                cmd.Parameters.Add("@comisionProforma", SqlDbType.Decimal, 15, 2).Value = pVendedor.comisionProforma
                cmd.Parameters.Add("@comisionSeparacion", SqlDbType.Decimal, 15, 2).Value = pVendedor.comisionSeparacion
                cmd.Parameters.Add("@comisionVenta", SqlDbType.Decimal, 15, 2).Value = pVendedor.comisionVenta
                cmd.Parameters.Add("@email", SqlDbType.VarChar, 40).Value = pVendedor.email
                cmd.Parameters.Add("@estado", SqlDbType.Char, 1).Value = pVendedor.estado
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pVendedor.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Eliminar(ByVal descEmpresa As String, ByVal pVendedor As Evendedor) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_vendedor_del", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idVendedor", SqlDbType.Int).Value = pVendedor.idVendedor
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pVendedor.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function

    Public Function datCargar_vendedores(ByVal strEmpresa As String) As DataTable
        Dim strConexionSQL As String = ConfigurationManager.ConnectionStrings(strEmpresa).ConnectionString
        Return SqlHelper.ExecuteDataTable(strConexionSQL, CommandType.StoredProcedure, "usp_listar_vendedores")
    End Function

End Class
